import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:study_online/entity/Student.dart';
import 'package:study_online/global/global.dart';
import 'package:study_online/pages/loginRegisterPage/LoginRegisterModel.dart';
import 'package:study_online/utils/rsa.dart';

import '../../main.dart';

class LoginViewModel extends ChangeNotifier{
  void login(String user, String pass) async {
    Response result = await loginModel(user, pass);//连接model
    print("###"+result.toString());
    String encode=encodeString(pass);
    if(result.data['code']==1){
      if(judge(encode, result.data['data']['pwd'])){
        Global.getInstance().user=Student.fromJson(result.data['data']);
        //print(Global.getInstance().user.name);
        EasyLoading.showToast("登录成功",toastPosition: EasyLoadingToastPosition.bottom);
        Navigator.of(navigatorKey.currentContext).popAndPushNamed("app");
      }
      else
        EasyLoading.showError("密码错误");
    }
    else
      EasyLoading.showError(result.data["msg"]);
  }
}
class RegisterViewModel extends ChangeNotifier{
  void register(String tel, String sno,String pwd) async {
    Response result = await registerModel(tel, sno,pwd);//连接model
    // if(result.data["code"]==1){
    //   Navigator.of(navigatorKey.currentContext).popAndPushNamed("app");
    // }
  }
}